package com.vvlive.caibao.smartcity.ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/11/19.
 * 投票页面
 */
public class VoteActivity extends BaseActivity {

    private CtrlCallBack mCallBack;
    private RadioGroup mChoices;
    private Button mVoteSubmit;

    private int mCheckedId;
    /**
     * 被邀请参加投票的人数
     */
    private int mNumberOfInvited;

    /**
     * 已经投票的人数
     */
    private int mNumberOfVoted;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_vote;
    }

    @Override
    public void initWidget() {
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle(R.string.func_vote);
        actionBar.setMoreVisible(false);

        mChoices = (RadioGroup) this.findViewById(R.id.vote_choices);
        mVoteSubmit = (Button) this.findViewById(R.id.vote_submit);

        mChoices.setOnCheckedChangeListener(getCallBack());
        mVoteSubmit.setOnClickListener(getCallBack());
    }

    /**
     * 获得回调函数
     *
     * @return
     */
    private CtrlCallBack getCallBack() {
        if (mCallBack == null) {
            mCallBack = new CtrlCallBack();
        }
        return mCallBack;
    }

    /**
     * 操作的回调函数
     */
    final class CtrlCallBack implements OnCheckedChangeListener, OnClickListener {

        private ProgressDialog mProgressDialog;
        private AlertDialog mResultDialog;

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            mCheckedId = checkedId;
        }

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.vote_submit) {
                if (mProgressDialog == null) {
                    mProgressDialog = ProgressDialog.show(VoteActivity.this, "", "正在获取投票结果");
                }
                mProgressDialog.show();
                v.postDelayed(mRunnable, 2000);
            } else if (id == R.id.vote_confirm) {
                mResultDialog.dismiss();
            }

        }

        private Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                mProgressDialog.dismiss();
                // 显示投票结果
                if (mResultDialog == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(VoteActivity.this);
                    LinearLayout content = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_vote_result, null);
                    builder.setView(content);
                    mResultDialog = builder.create();
                    content.findViewById(R.id.vote_confirm).setOnClickListener(CtrlCallBack.this);
                }
                mResultDialog.show();
            }
        };
    }

    /**
     * 获取被邀请投票的人数
     *
     * @return
     */
    private int getNumberOfInvited() {
        return 10;
    }

    /**
     * 获取已经投票的人数
     *
     * @return
     */
    private int getNumberOfVoted() {
        return 5;
    }

}
